//
//  DMTextCycle.m
//  test1
//
//  Created by xmy on 16/9/24.
//  Copyright © 2016年 xmy. All rights reserved.
//
#define topViewHeight 200
#define cycleTextHeight 40

#import "DMTextCycle.h"
#import "Masonry.h"
#import "YYModel.h"
#import "DMTextCycleModel.h"
#import "DMWNetworkTools.h"

@interface DMTextCycle ()<UIScrollViewDelegate,UIWebViewDelegate>
@property (weak,nonatomic) UIImageView *seperatorView;
@property (weak,nonatomic) UIScrollView *scrollView;
@property (weak,nonatomic) NSTimer *timer;
@property (weak,nonatomic) UILabel *titleLabel;

@property(nonatomic,strong) NSArray *cycleList;

@end
@implementation DMTextCycle

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}
-(void)awakeFromNib{
    [super awakeFromNib];
    [self setup];
}
- (void)setup{
    //获取数据
    [self loadData];
    [self getCycleLabel];
    //创建定时器
    [self setupTimer];
}
- (void)loadData{
    
    //4. 获取数据模型
    NSArray *categoryPlist = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]  pathForResource:@"TextCycle.plist" ofType:nil]];
    
    self.cycleList = [NSArray yy_modelArrayWithClass:NSClassFromString(@"DMTextCycleModel") json:categoryPlist];
    
    
}

#pragma mark - 设置定时器
- (void)setupTimer{
    NSTimer *timer = [NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(cycleLabel) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
    _timer = timer;
}

#pragma mark - 执行定时器方法
- (void)cycleLabel{
    CGPoint offset = self.scrollView.contentOffset;
    offset.y += self.scrollView.bounds.size.height;
    
    [_scrollView setContentOffset:offset animated:YES];
    
    //判断 实现无限轮播
    if (offset.y == (_cycleList.count ) *self.scrollView.bounds.size.height) {
        [_scrollView setContentOffset:CGPointZero animated:NO];
    }
}
#pragma mark - 文字轮播视图
- (void)getCycleLabel{
    
    self.backgroundColor = [UIColor whiteColor];
    //顶部分割线
    UIImageView *topSeperatorView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ticket_detail_list_bottom"]];
    [self addSubview:topSeperatorView];
    
    [topSeperatorView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
        make.left.right.equalTo(self);
        make.height.equalTo(@1);
    }];

    
    //大麦头条
    UIImageView *notificationView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"index_damaiheadlines"]];
    [self addSubview:notificationView];
    
    [notificationView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(5);
        make.bottom.equalTo(self).offset(-7);
        make.left.equalTo(self.mas_left).offset(15);
        make.width.equalTo(@75);
    }];
    
    //隔断
    UIImageView *seperatorView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bar"]];
    _seperatorView = seperatorView;
    [self addSubview:seperatorView];
    
    [seperatorView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(notificationView.mas_right).offset(10);
        make.top.equalTo(self).offset(7);
        make.bottom.equalTo(self).offset(-15);
        make.width.equalTo(@1);
    }];
    
    //右部文字轮播
    [self setCycleLabel];
    
}


#pragma mark - 设置文字轮播
- (void)setCycleLabel{
    //设置scrollView-->实现文字轮播根View
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    _scrollView = scrollView;
    [self addSubview:scrollView];
    
    //属性设置
    //设置scrollView不能用户交互
//    scrollView.userInteractionEnabled = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.bounces = NO;
    scrollView.panGestureRecognizer.enabled = NO;
    scrollView.pagingEnabled = YES;
    scrollView.delegate = self;
    
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_seperatorView.mas_right).offset(10);
        make.top.equalTo(self);
        make.right.bottom.equalTo(self);
    }];
    
    //滚动标题Label
    NSMutableArray<UIButton *> *tempArr = [NSMutableArray array];
    [_cycleList enumerateObjectsUsingBlock:^(DMTextCycleModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        UIButton *btn = [[UIButton alloc] init];
        [scrollView addSubview:btn];
        
        btn.tag = idx;
        [btn setTitle:obj.text forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:13];

        [btn addTarget:self action:@selector(textCycleClick:) forControlEvents:UIControlEventTouchUpInside];
//        textLabel.text = obj.text;
//        textLabel.font = [UIFont systemFontOfSize:13];
//        textLabel.numberOfLines = 1;
        
        [tempArr addObject:btn];
    }];
    _cycleList = tempArr;
    
    //布局
    [tempArr mas_distributeViewsAlongAxis:MASAxisTypeVertical withFixedSpacing:0 leadSpacing:0 tailSpacing:0];
    [tempArr mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(scrollView).offset(10);
        make.height.equalTo(@cycleTextHeight);
    }];
    
}

//btn的点击效果
- (void)textCycleClick:(UIButton *)btn{
    
    NSLog(@"%@---%ld",btn.titleLabel.text,(long)btn.tag);

    
//            DMTextCycleModel *model = _cycleList[btn.tag];
//            
//            NSString *str = model.value;
//            
//            [self webViewLoad:str];
//    
    
}
- (void)webViewLoad:(NSString *)urlStr{
    
    UIWebView *webView = [[UIWebView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    
    webView.delegate = self;
    
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlStr]]];
    
}
- (void)webViewDidStartLoad:(UIWebView *)webView;{
    NSLog(@"正在加载");
}
- (void)webViewDidFinishLoad:(UIWebView *)webView;{
    NSLog(@"加载完成");
}
- (void)dealloc{
    [self.timer invalidate];
}



@end
